import type { RainbowDataType, SettingType } from '../../interface'

/** 接口数据转换成图表数据格式 */
export const transformChart = (data: RainbowDataType[]) => {
    const newArr = data.map(item => {
        // 将大于1的投入程度拆分成多个投入程度
        const settings: SettingType[] = []
        item.settings.forEach(i => {
            if (Number(i.grade) > 1) {
                for (let index = 1; index <= Number(i.grade); index++) {
                    settings.push({ ...i, grade: index })
                }
            } else {
                settings.push(i)
            }
        })
        // 按照投入程度从小到大排序；相同投入程度的按照年龄从小到大排序
        settings.sort((a, b) => {
            if (Number(a.grade) === Number(b.grade)) {
                return Number(a.age_end) - Number(b.age_end)
            }
            return Number(a.grade) - Number(b.grade)
        })
        // 将投入程度相同的串联起来
        const newSettings: SettingType[] = []
        let index = 0
        settings.map(i => {
            if (newSettings[index]) {
                if (newSettings[index].grade === i.grade && Number(newSettings[index].age_end) + 1 === Number(i.age_start)) {
                    newSettings[index].age_end = i.age_end
                } else {
                    newSettings.push(i)
                    index++
                }
            } else {
                newSettings.push(i)
            }
        })
        return {
            ...item,
            settings: newSettings,
        }
    })
    return newArr
}
/** 图表数据转换成接口数据格式 */
export const transformData = (data: RainbowDataType[]) => {
    const newArr = data.map(item => {
        const settings = [...item.settings]
        // 按照投入程度从大到小排序
        settings.sort((a, b) => {
            return Number(b.grade) - Number(a.grade)
        })
        const newSettings: SettingType[] = []
        settings.map(i => {
            /** 是否经过处理 */
            let isDeal = false
            const arr: SettingType[] = []
            // 将重叠部分的年龄段记录下拉
            for (let index = 0; index < newSettings.length; index++) {
                const element = newSettings[index]
                if (Number(element.grade) > Number(i.grade) && Number(element.age_start) >= Number(i.age_start) && Number(element.age_end) <= Number(i.age_end)) {
                    isDeal = true
                    arr.push({ ...i, age_start: element.age_start, age_end: element.age_end })
                }
            }
            // 没有分割处理的直接存进最终数组
            if (!isDeal) {
                newSettings.push(i)
            }
            if (arr.length === 0) return
            // 将重叠部分的年龄段从小到大排序
            arr.sort((a, b) => {
                return Number(a.age_start) - Number(b.age_start)
            })
            let start = i.age_start,
                end = String(Number(arr[0].age_start) - 1)
            // 去除单项中重叠的部分年龄段，并将剩余的存入最终数组
            for (let index = 0; index <= arr.length; index++) {
                if (Number(start) <= Number(end)) {
                    newSettings.push({ ...i, age_start: start, age_end: end })
                }
                if (index === arr.length) return
                start = String(Number(arr[index].age_end) + 1)
                end = index === arr.length - 1 ? i.age_end : String(Number(arr[index + 1].age_start) - 1)
            }
        })
        // 按照年龄从小到大排序, 将开始年龄和结束年龄相同的去除掉，并将sort字段重新赋值
        const copySettings = newSettings
            .sort((a, b) => {
                return Number(a.age_start) - Number(b.age_start)
            })
            .filter(ele => ele.age_start !== ele.age_end)
            .map((i, idx) => {
                return { ...i, sort: idx + 1 }
            })
        return {
            ...item,
            age_start: copySettings[0].age_start,
            age_end: copySettings[copySettings.length - 1].age_end,
            settings: copySettings,
        }
    })
    console.log('transformData', newArr)
    return newArr
}
